package org.dromara.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;
import org.dromara.system.mapper.SysDeptMapper;
import org.dromara.system.pojo.bo.SysDeptBO;
import org.dromara.system.pojo.entity.SysDeptDO;
import org.dromara.system.pojo.qo.SysDeptQO;
import org.dromara.system.pojo.vo.SysDeptVO;
import org.dromara.system.service.SysDeptService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 部门表 服务层实现。
 *
 * @author mkj
 * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysDeptServiceImpl implements SysDeptService {

    private final SysDeptMapper sysDeptMapper;

    @Override
    public boolean add(SysDeptBO sysDeptBO) {
        SysDeptDO sysDeptDO = BeanCopyUtils.copy(sysDeptBO, SysDeptDO.class);
        return sysDeptMapper.insert(sysDeptDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysDeptMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysDeptBO sysDeptBO) {
        SysDeptDO sysDeptDO = BeanCopyUtils.copy(sysDeptBO, SysDeptDO.class);
        return sysDeptMapper.update(sysDeptDO) > 0;
    }

    @Override
    public List<SysDeptVO> list(SysDeptQO sysDeptQO) {
        SysDeptDO sysDeptDO = BeanCopyUtils.copy(sysDeptQO, SysDeptDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysDeptDO);
        return sysDeptMapper.selectListByQueryAs(queryWrapper, SysDeptVO.class);
    }

    @Override
    public SysDeptVO getInfo(Long id) {
        SysDeptDO sysDeptDO = sysDeptMapper.selectOneById(id);
        if (sysDeptDO == null) {
            return null;
        }
        return BeanCopyUtils.copy(sysDeptDO, SysDeptVO.class);
    }

    @Override
    public Page<SysDeptVO> page(SysDeptQO page) {
        return sysDeptMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysDeptVO.class);
    }

}

